#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time  : 2019-12-05 16:42
# @Author: Simon
# @File  : luck_turntable.
import cgi
import random
# 一元夺宝
import tkinter
import webbrowser
from tkinter import *
import tkinter.messagebox
# 创建窗口对象
import login
class Lucky():

    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('一元夺宝')
        self.root.minsize(300, 300)
        self.name = ''
        self.i = 0
        self.j = 0
        self.k = 0
        self.tc = FALSE
        self.filename = 'G:\\PycharmProjects\\lucky\\images\\luck.png'
        # 滚动条
        self.scroll = Scrollbar(self.root)
        # 设置文本大小
        self.text = tkinter.Text(self.root, width=33, height=20)
        self.prize1 = "玛莎拉蒂总裁"
        self.prize2 = "捷豹XJL"
        self.prize3 = "iPhone11"
        self.prize4 = "iWatch3"
        self.prize5 = "小米充电宝"
        self.prize6 = "下次再来"

    def todo(self):
        dic1 = {"name": self.prize1, "minValue": 0, "maxValue": 5}
        dic2 = {"name": self.prize2, "minValue": 6, "maxValue": 15}
        dic3 = {"name": self.prize3, "minValue": 16, "maxValue": 50}
        dic4 = {"name": self.prize4, "minValue": 51, "maxValue": 100}
        dic5 = {"name": self.prize5, "minValue": 101, "maxValue": 1000}
        dic6 = {"name": self.prize6, "minValue": 1001, "maxValue": 10000}
        global lucky_value
        lucky_value = random.randint(0, 10000)
        # 存入列表
        tablelist = [dic1, dic2, dic3, dic4, dic5, dic6]
        for x in tablelist:
            if x.get("minValue") <= lucky_value <= x.get("maxValue"):
                print(lucky_value)
                self.name = x.get("name")
                break

    def show(self):
        # 用于文本显示结果
        # 触发弹出网页（第一次）
        if self.j == 0:
            self.tc = tkinter.messagebox.askyesno(title='授权验证~', message='请登录')
            self.open_url("<Button-1>")
            self.k = login.login()
            print("登录状态 = " + str(self.k))
            self.j = 1
            print("鉴权j = " + str(self.j))

        if self.j == 1:
            self.i += 1
            self.todo()
            # 设置文本颜色
            self.text.tag_config('tag_color_red', background='yellow', foreground='red')
            self.text.insert('insert', "本次是第: " + str(self.i) + "次" + "\n")
            if 0 <= lucky_value <= 1000:
                self.text.insert('insert', "幸运数字是：" + str(
                    lucky_value) + "\n" + "奖品是：" + self.name + "\n" + "===================" + "\n", 'tag_color_red')
                self.ok()
            else:
                self.text.insert('insert', "幸运数字是：" + str(
                    lucky_value) + "\n" + "奖品是：" + self.name + "\n" + "===================" + "\n")
            # 将滚动条填充 side是滚动条放置的位置，上下左右。fill是将滚动条沿着y轴填充
            self.scroll.pack(side=RIGHT, fill=Y)
            # 将文本框填充进窗口的左侧
            self.text.pack()
            # 滚动条始终处于最底部
            self.text.see(END)

    def ok(self):
        # 弹出对话框
        return tkinter.messagebox.askokcancel(title='恭喜你~', message='已中奖，点击查看')

    def open_url(self, event):
        webbrowser.open("http://127.0.0.1", new=0)

    def uitable(self):

        text1 = tkinter.Text(self.root, width=33, height=21)
        photo = PhotoImage(file=self.filename)
        text1.insert('insert', "1等奖: " + self.prize1 + "\n")
        text1.insert('insert', "2等奖: " + self.prize2 + "\n")
        text1.insert('insert', "3等奖: " + self.prize3 + "\n")
        text1.insert('insert', "4等奖: " + self.prize4 + "\n")
        text1.insert('insert', "5等奖: " + self.prize5 + "\n")
        # 插入图片
        text1.image_create(tkinter.END, image=photo)
        text1.pack(padx=10, pady=10)
        # 插入超链接
        link = tkinter.Label(self.root, text="提交数据", font=('Arial', 10))
        link.place(x=120, y=260)
        # 绑定label单击事件
        link.bind("<Button-1>", self.open_url)

        # 文本框关联到滚动条上，滚动条滑动，文本框跟随滑动
        self.scroll.config(command=self.text.yview)
        # 将滚动条关联到文本框
        self.text.config(yscrollcommand=self.scroll.set)

        # 创建按钮对象
        B = tkinter.Button(self.root, text='开始', command=self.show, fg="red")
        #text1.insert('insert', "测试 j = : " + str(self.j) + "\n")
        print(self.j)
        # 将小部件放置到主窗口中
        B.pack()
        # 进入消息循环
        self.root.mainloop()
